// course/components/create-or-edit.vue
<template>
  <el-card>
    <!-- 头部：步骤条 -->
    <div slot="header">
      <el-steps :active="activeStep" simple>
        <el-step
          v-for="(item, i) in steps"
          :key="item.id"
          :title="item.title"
          :icon="item.icon"
          @click.native="activeStep = i"
        ></el-step>
      </el-steps>
    </div>
    <!-- 步骤容器 -->
    <el-form label-width="80px">
      <!-- 基本信息 -->
      <div v-show="activeStep === 0">
        <el-form-item label="课程名称">
          <el-input v-model="course.courseName"></el-input>
        </el-form-item>
        <el-form-item label="课程简介">
          <el-input v-model="course.brief"></el-input>
        </el-form-item>
        <el-form-item label="课程概述">
          <el-input
            v-model="course.previewFirstField"
            placeholder="概述1"
            style="width: 49%;min-width: 300px; margin-right: 15px;"
          >
            <template slot="append">{{ course.previewFirstField.length }}/20</template>
          </el-input>
          <el-input
            v-model="course.previewSecondField"
            placeholder="概述2"
            style="width: 49%;min-width: 300px;"
          >
            <template slot="append">{{ course.previewFirstField.length }}/20</template>
          </el-input>
        </el-form-item>
        <el-form-item label="讲师姓名">
          <el-input v-model="course.teacherDTO.teacherName"></el-input>
        </el-form-item>
        <el-form-item label="讲师简介">
          <el-input  v-model="course.teacherDTO.description"></el-input>
        </el-form-item>
        <el-form-item label="课程排序">
          <el-input-number
            v-model="course.sortNum"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
      </div>
      <!-- 课程封面 -->
      <div v-show="activeStep === 1">
        <!-- 课程封面图上传 -->
        <course-image v-model="course.courseListImg" :limit="5"></course-image>
        <!-- 解锁封面图上传 -->
        <course-image v-model="course.courseImgUrl" :limit="10"></course-image>
      </div>
      <!-- 销售信息 -->
      <div v-show="activeStep === 2">
        <el-form-item label="售卖价格">
          <el-input
            type="number"
            v-model="course.discounts"
            :min="0"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="商品原价">
          <el-input
            type="number"
            v-model="course.price"
            :min="0"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="销量">
          <el-input
            type="number"
            v-model="course.sales"
            :min="0"
          >
            <template slot="append">单</template>
          </el-input>
        </el-form-item>
        <el-form-item label="活动标签">
          <el-input
            v-model="course.discountsTag"
          ></el-input>
        </el-form-item>
      </div>
      <!-- 秒杀活动 -->
      <div v-show="activeStep === 3">
        <!-- 设置秒杀状态开关 -->
        <el-form-item label="限时秒杀开关" label-width="120px">
          <el-switch
            v-model="course.activityCourse"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <template v-if="course.activityCourse">
          <!-- 其他部分的基础结构 -->
          <el-form-item label="开始时间">
          <!-- <el-input></el-input> -->
          <el-date-picker
            v-model="course.activityCourseDTO.beginTime"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <!-- <el-input></el-input> -->
          <el-date-picker
            v-model="course.activityCourseDTO.endTime"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="秒杀价">
          <el-input
            v-model="course.activityCourseDTO.amount"
            type="number"
            :min="0"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="秒杀库存">
          <el-input
            v-model="course.activityCourseDTO.stock"
            type="number"
            :min="0"
          >
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        </template>
      </div>
      <!-- 课程详情 -->
      <div v-show="activeStep === 4">
        <el-form-item label="课程详情">
          <!-- <el-input
            type="textarea"
            v-model="course.courseDescriptionMarkDown"
          ></el-input> -->
          <text-editer v-model="course.courseDescriptionMarkDown"></text-editer>
        </el-form-item>
        <!-- 增加上架开关 -->
        <el-form-item label="是否上架">
          <el-switch
            v-model="course.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <!-- 最后步骤中设置保存按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSave"
          >保存</el-button>
        </el-form-item>
      </div>
      <!-- 下一步 -->
      <el-form-item v-if="activeStep !== steps.length - 1">
        <el-button @click="activeStep++">下一步</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
// 引入图片上传组件（单独封装 create-or-edit.vue 后，修改为同级目录）
import CourseImage from './course-img'
// 引入公共组件
import TextEditer from './TextEditer/index'

export default {
  name: 'CreateOrEdit',
  components: {
    CourseImage,
    TextEditer
  },
  props: {
    courseId: {
      type: [String, Number]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 步骤条进度
      activeStep: 0,
      // 步骤条信息
      steps: [
        { id: 1, title: '基本信息', icon: 'el-icon-edit' },
        { id: 2, title: '课程封面', icon: 'el-icon-upload' },
        { id: 3, title: '销售信息', icon: 'el-icon-picture' },
        { id: 4, title: '秒杀信息', icon: 'el-icon-picture' },
        { id: 5, title: '课程详情', icon: 'el-icon-picture' }
      ],
      // 本地预览图片地址
      imageUrl: '',
      // 秒杀状态
      // isSeckill: false,
      // 保存课程信息
      course: {
        // id: 0,
        courseName: '',
        brief: '',
        teacherDTO: {
          // id: 0,
          // courseId: 0,
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '<h3>请输入课程详情</h3>',
        price: 0,
        discounts: 0,
        priceTag: '',
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        courseListImg: '',
        courseImgUrl: '',
        sortNum: 0,
        previewFirstField: '',
        previewSecondField: '',
        status: 0,
        sales: 0,
        activityCourse: false,
        activityCourseDTO: {
          // id: 0,
          // courseId: 0,
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0
        },
        autoOnlineTime: ''
      }
    }
  },
  methods: {
    // handleAvatarSuccess、beforeAvatarUpload、handleUpload 均封装到子组件中
    async handleSave () {
      // 发送请求
      const { data } = await saveOrUpdateCourse(this.course)
      if (data.code === '000000') {
        this.$router.push({ name: 'course' })
        this.$message.success('添加课程成功')
      }
    },
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        this.course = data.data
      }
    }
  },
  created () {
    if (this.isEdit) {
      // 如果是编辑
      this.loadCourse()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-step {
  cursor: pointer
}
</style>
